﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;

namespace JobSeek.Classes
{
    public class ImageControl
    {
        public static void ScaleImage(Image upBmp, string destination, int rawHeight, int rawWidth)
        {
            int[] dimension = Resize(rawWidth, rawHeight, upBmp);

            Bitmap createdImage = CreateFinalImage(rawWidth, rawHeight, upBmp, dimension[2], dimension[3], dimension[0], dimension[1]);


            createdImage.Save(HttpContext.Current.Server.MapPath(destination), System.Drawing.Imaging.ImageFormat.Png);
            createdImage.Dispose();

        }

        private static int[] Resize(double rawWidth, double rawHeight, Image imgToResize)
        {
            double newHeight = rawHeight;
            double newWidth = rawWidth;

            double newX = 0;
            double newY = 0;

            //need to check if this is landscape of portrait;
            if (imgToResize.Width > imgToResize.Height)
            {
                if (imgToResize.Width > rawWidth)
                {
                    double ratio = rawWidth / imgToResize.Width;

                    newHeight = Convert.ToInt32(imgToResize.Height * ratio);
                    //newWidth will be 131;
                }
            }
            //this is portrait
            else if (imgToResize.Height > imgToResize.Width)
            {
                if (imgToResize.Height > rawHeight)
                {
                    double ratio = rawHeight / imgToResize.Height;
                    newWidth = Convert.ToInt32(imgToResize.Width * ratio);
                    //newHeight will be 131;
                }
            }
            // this will be smaller than the 131 x 131
            else
            {
                newWidth = imgToResize.Width;
                newHeight = imgToResize.Height;

            }
            newX = (rawWidth - newWidth) / 2;
            newY = (rawHeight - newHeight) / 2;


            int[] dimension = new int[4];

            dimension[0] = Convert.ToInt32(newWidth);
            dimension[1] = Convert.ToInt32(newHeight);
            dimension[2] = Convert.ToInt32(newX);
            dimension[3] = Convert.ToInt32(newY);

            return dimension;
        }

        public static void CropImage(Image source, string destination, string oldImagePath, int x, int y, int x2, int y2, int width, int height, string format)
        {
            int cropWidth = x2 - x;
            int cropHeight = y2 - y;

            Rectangle cropArea = new Rectangle(x, y, cropWidth, cropHeight);

            Bitmap cropBmp = new Bitmap(cropWidth, cropHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Graphics graphic = Graphics.FromImage(cropBmp);
            graphic.Clear(Color.White);
            graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            graphic.DrawImage(source, new Rectangle(0, 0, cropWidth, cropHeight), cropArea, GraphicsUnit.Pixel);

            //Dispose
            graphic.Dispose();
            source.Dispose();

            //resize the cropBmp to the specified size that I want
            int[] dimension = Resize(width, height, cropBmp);
            Bitmap resizeCropImg = CreateFinalImage(width, height, cropBmp, dimension[2], dimension[3], dimension[0], dimension[1]);

            //Dispose before it can be saved
            cropBmp.Dispose();

            if (File.Exists(HttpContext.Current.Server.MapPath(oldImagePath)))
            {
                File.Delete(HttpContext.Current.Server.MapPath(oldImagePath));
            }

            resizeCropImg.Save(HttpContext.Current.Server.MapPath(destination), System.Drawing.Imaging.ImageFormat.Png);

           resizeCropImg.Dispose();

        }

        private static Bitmap CreateFinalImage(int rawWidth, int rawHeight, Image upBmp, int newX, int newY, int newWidth, int newHeight)
        {
            Bitmap newBmp = new Bitmap(Convert.ToInt32(rawWidth), Convert.ToInt32(rawHeight), System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Graphics graphic = Graphics.FromImage(newBmp);
            graphic.Clear(Color.White);
            graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            graphic.DrawImage(upBmp, Convert.ToInt32(newX), Convert.ToInt32(newY), Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));

            //Dispose the uploaded Bitmap to enable overwriting and deleting
            upBmp.Dispose();
            graphic.Dispose();
            return newBmp;
           
        }

        public static bool CheckFileContentType(string contentType)
        {
            bool check = false;
            switch (contentType)
            {
                case "image/jpeg":
                    check = true;
                    break;
                case "image/png":
                    check = true;
                    break;
                case "image/bmp":
                    check = true;
                    break;
                case "image/gif":
                    check = true;
                    break;
                default:
                    check = false;
                    break;
            }

            return check;
        }
    }
}